/*
 *
 * Copyright(c) 2010 Safelayer Secure Communications
 *
 * This is open source software, placed under the terms of the
 * GNU GENERAL PUBLIC LICENSE Version 3.
 * Please see: http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Permission to use, copy, modify, and distribute this software
 * for any purpose and without fee is hereby granted, provided
 * that the above copyright notices appear in all copies and that
 * both the copyright notice and this permission notice appear in
 * supporting documentation.
 * 
 */
package com.neuralnoise.foaf.core;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Models a FOAF Group. The <code>Group</code> class represents a collection of individual agents
 * (and may itself play the role of a {@link Agent}, ie. something that can perform actions).
 * <p>
 * For more info see FOAF Vocabulary Specification
 * 
 * @author victor.martinez <swtp@safelayer.com>
 */
public class Group extends Agent {

	private Set<Agent> memberSet = new LinkedHashSet<Agent>();

	// include membershipClass ??

	public Group() {
		super(null);
	}

	public Group(String uri) {
		super(uri);
	}

	/**
	 * Returns all members.
	 * 
	 * @return Returns all members.
	 */
	public List<Agent> getAllMembers() {
		return new ArrayList<Agent>(memberSet);
	}

	/**
	 * Adds the specified <code>member</code> to this {@link Group} if it is not already present.
	 * 
	 * @param member The <code>member</code> to be added to this <code>Group</code> .
	 * @return <tt>true</tt> if the <code>Group</code>. did not already contain the specified
	 *         <code>member</code>.
	 */
	public boolean addMember(Agent member) {
		return memberSet.add(member);
	}

	/**
	 * Removes the specified <code>member</code> from this {@link Group}, if it is present. Returns
	 * <tt>true</tt> if this <code>Group</code> contained the specified <code>member</code> (or
	 * equivalently, if this <code>Group</code> changed as a result of the call).
	 * 
	 * @param member The <code>member</code> to be removed from this <code>Group</code>.
	 * @return <tt>true</tt> if this <code>Group</code> contained the specified <code>member</code>.
	 */
	public boolean removeMember(Agent member) {
		return memberSet.remove(member);
	}

	/**
	 * Removes all of the members from this {@link Group}. The members from <code>Group</code> will
	 * be empty after this call returns.
	 */
	public void clearMembers() {
		memberSet.clear();
	}

	/**
	 * Returns <tt>true</tt> if this {@link Group} contains the specified <code>member</code>
	 * 
	 * @param member The <code>member</code> whose presence in this <code>Group</code> is to be
	 *            tested.
	 * @return <tt>true</tt> if this <code>Group</code> contains the specified <code>member</code>.
	 */
	public boolean containsMember(Agent member) {
		return memberSet.contains(member);
	}
}
